package com.kyexpress.vms.markdown;

import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.kyexpress.vms.enitity.DataCenter;
import com.kyexpress.vms.enitity.MyConfig;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.Writer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class MDFreeMarkProcessor {
    
      protected static Template getTemplate() throws Exception {
          //加载模板字符串
          String templateString = UrlUtil.loadText(MDFreeMarkProcessor.class.getResource("/com/kyexpress/vms/markdown/template/md.ftl"));
          //创建模板配置
          Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
          //创建字符串模板的导入器
          StringTemplateLoader stringTemplateLoader=new StringTemplateLoader();
          //导入字符串模板
          stringTemplateLoader.putTemplate("MDTemplate",templateString);
          configuration.setTemplateLoader(stringTemplateLoader);
          //获取模板
          return configuration.getTemplate("MDTemplate");
      }
  
   
      protected static Object getModel(String fileName) {
          HashMap model = new HashMap();
          model.put("packageUrl", MyConfig.getInstance().package_name);
          model.put("fileName", fileName);
          model.put("author", MyConfig.getInstance().author);
          model.put("dateTime", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
          model.put("description", DataCenter.DESCRIPTION);
          model.put("lookUpCode", DataCenter.SELECT_TEXT);
          model.put("dictList",DataCenter.DICT_LIST);
          return model;
      }
  
      
    

    public static void process(Writer writer,String fileName) throws Exception {
        Template template = getTemplate();
        Object model = getModel(fileName);
        template.process(model, writer);
    }
  
  }
